package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.OrderInfo;
import bean.User;
import common.BaseServlet;
import service.AdminOrderDAO;

/**
 * Servlet implementation class AdminOrder
 */
@WebServlet("/AdminOrder")
public class AdminOrder extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private final int pagesize=2;//分页的单页数据条数
	AdminOrderDAO aoDAO=new AdminOrderDAO();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminOrder() {
        super();
        // TODO Auto-generated constructor stub
    }

	@Override
	public String basefun(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");

		PrintWriter out;
		out = response.getWriter();
		//获取页数
		String viewStr=request.getParameter("viewPage");
		int viewPage=(viewStr==null)?1:Integer.parseInt(viewStr);
		request.setAttribute("currentPage", viewPage);
    	out.println("viewPage:"+viewPage);
    	List<OrderInfo> list=new ArrayList<OrderInfo>();
		
    	//获取总订单数据
		list=aoDAO.queryAllOrders(pagesize,viewPage);
		System.out.println(list.size());
		//获取订单数据总条数
		int listCount=aoDAO.listAll();
		request.setAttribute("listCount", listCount);
		HttpSession session=request.getSession();
		session.setAttribute("list", list); 
		
		StringBuffer itemsList=new StringBuffer();
		itemsList.append("[");
		for(int i=0;i<list.size();i++) {
			OrderInfo order=list.get(i);
			itemsList.append("{\"orderid\":\""+order.getOrderid()+"\",");
			itemsList.append("\"payStatus\":\""+order.getPayStatus()+"\",");
			itemsList.append("\"postStatus\":\""+order.getPostStatus()+"\",");
			itemsList.append("\"orderStatus\":\""+order.getOrderStatus()+"\",");
			itemsList.append("\"postFee\":\""+order.getPostFee()+"\",");
			itemsList.append("\"createTime\":\""+order.getCreateTime()+"\",");
			
			String nickname=aoDAO.getUser(order.getUserid()).get(0).getNickname();
			String name=aoDAO.getAddress(order.getAddressid()).get(0).getConsignee();
			itemsList.append("\"nickname\":\""+nickname+"\",");
			if (i==list.size()-1) {
				itemsList.append("\"name\":\""+name+"\"}");
			}
			else {
				itemsList.append("\"name\":\""+name+"\"},");
			}
		}
		itemsList.append("]");
		request.setAttribute("items",new String(itemsList));
		
		return "f:htjsp/OrderList";
	}
	
	//订单批量删除（伪）方法
		public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException{
			PrintWriter out;
			out=response.getWriter();
			String str=request.getParameter("str");
			if(str.equals("")) {
				out.print("fail");
			}
			else {
			String[] orderid=str.split(",");
			for(int i=0;i<orderid.length;i++) {
				aoDAO.delete(orderid[i]);
			}
			out.print("ok");
			}
		}
		
		//订单批量发货方法
		public void sendGood(HttpServletRequest request, HttpServletResponse response) throws IOException{
			PrintWriter out;
			out=response.getWriter();
			String str=request.getParameter("str");
			if(str.equals("")) {
				out.print("fail");
			}
			else {
			String[] orderid=str.split(",");
			for(int i=0;i<orderid.length;i++) {
				aoDAO.sendGood(orderid[i]);
			}
			out.print("ok");
			}
		}
		

}
